/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bookshop.customControllers;

import com.bookshop.DTO.CartItem;
import com.bookshop.DTO.shoppingCartBeanLocal;
import com.bookshop.entity.Book;
import com.bookshop.facade.BookFacade;
import com.bookshop.facade.CustomerorderBookFacade;
import com.bookshop.facade.CustomerorderFacade;
import java.util.List;
import java.util.Map;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

/**
 *
 * @author Samir
 */
@ManagedBean(name = "mbCart")
@SessionScoped
public class MyCartController {

    @EJB
    private shoppingCartBeanLocal shoppingCartBean;
//    @EJB
//    private CategoryFacade categoryFacade;
    @EJB
    private BookFacade bookFacade;
    @EJB
    private CustomerorderBookFacade customerOrderBookFacade;
    @EJB
    private CustomerorderFacade customerOrderFacade;

    /**
     * Creates a new instance of myCartController
     */
    public String addItemToCart() throws Exception {
        Book item = bookFacade.getBookByID(this.getParamId("isbn"));
        shoppingCartBean.addItem(item);
        return "index.xhtml";
    }

    @SuppressWarnings("empty-statement")
    public String removeItem() throws Exception {
        Book item = bookFacade.getBookByID(this.getParamId("isbn"));
        shoppingCartBean.removeItem(item);
        if (shoppingCartBean.getCartItems().isEmpty()) {
            return "index.xhtml";
        };
        return null;
    }

    public String checkout() {
        return "index.xhtml";
    }

    public String getTotal() {
        return shoppingCartBean.getTotal();
    }

    public List<CartItem> getCartItems() {
        return shoppingCartBean.getCartItems();
    }

    public int getItemNumber() {
        int result = shoppingCartBean.getItemNumberFromCart();
        return result;
    }

    public String affichePanier() {
        if(shoppingCartBean.getCartItems().isEmpty()){
            return null;
        }else{
            return "myCart.xhtml";
        }
    }

//    public String confirmOrder(){
//          TODO
//        return "";
//    }
    /**
     * methode qui récupere les paramaitres passés dans les *.xhtml
     *
     * @param param
     * @return
     */
    protected String getParamId(String param) {
        FacesContext context = FacesContext.getCurrentInstance();
        Map<String, String> map = context.getExternalContext().getRequestParameterMap();
        String result = map.get(param);
        System.out.println("*****************param = " + result + "******************");
        return result;
    }
}
